//
//  MeatShopBottomer.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/20.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatShopBottomer.h"

#define selfH (TJ_SafeAreaBottomHeight + TJ_MeatShop_Bottomer + 12)

@interface MeatShopBottomer()

/**
 MeatShopBottomer 's ContentView -> UIView
 */
@property (nonatomic, strong) UIView *contentView;


/**
 MeatShopBottomer 's PayBtn -> UIButton
 */
@property (nonatomic, strong) UIButton *payBtn;


/**
 MeatShopBottomer 's PayLab -> UILabel
 */
@property (nonatomic, strong) UILabel *payLab;

@end


@implementation MeatShopBottomer

- (void)setupUI{
    
    self.backgroundColor = KClearColor;
    
    [self addSubview:self.contentView];
    [self addSubview:self.cartIcon];
    [self addSubview:self.payBtn];
    [self addSubview:self.payLab];
    
    [_contentView.layer addSublayer:self.line];
    self.line.hidden = NO;
    [_cartIcon addSubview:self.cartTipNumberLab];

}

- (void)setupLayout{
  
    self.height = selfH;
    
    {
        _contentView.size = CGSizeMake(KScreenWidth, selfH - 12);
        _contentView.mj_y = 12;
        _contentView.mj_x = 0;
    }
    
   
    {
        CGFloat adapterW = 50 + TJ_SafeAreaBottomHeight/2.0;
        _cartIcon.size = CGSizeMake( adapterW, adapterW);
        _cartIcon.mj_y = 0;
        _cartIcon.mj_x = 15;
    }
    
    {
        _payLab.mj_x = _cartIcon.right + 10;
        _payLab.height = 18;
        _payLab.width = KScreenWidth - _cartIcon.right - _payBtn.width- 20;
        _payLab.mj_y = _cartIcon.bottom - 3 - _payLab.height;
    }
    
    {
        _payBtn.size = CGSizeMake(125, selfH - 12);
        _payBtn.mj_x = KScreenWidth - _payBtn.width;
        _payBtn.mj_y = 12;
    }
    
    {
        _cartTipNumberLab.size = CGSizeMake(18, 18);
        _cartTipNumberLab.mj_y = 0;
        _cartTipNumberLab.mj_x = _cartIcon.width - _cartTipNumberLab.width +5;
    }
    
    [_payLab setAttributedText:[self getPayText:@"1436.78"]];

}


#pragma mark - setter and getter

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}

- (UIImageView *)cartIcon{
    
    if (!_cartIcon) {
        _cartIcon = [UIImageView new];
        _cartIcon.image = [UIImage imageNamed:@"meat_shop_cart_light"];
        
        kWeakSelf(self);
        _cartIcon.tapBlock = ^{
           
            if (weakself.dealShowCartBlock) {
                weakself.dealShowCartBlock();
            }
        };
    }
    return _cartIcon;
}


- (UIButton *)payBtn{
    
    if (!_payBtn) {
        _payBtn = [UIButton makeButtonWithText:@"去结算" textColor:KWhiteColor font:mFont(17) textAliment:1 cornerRadius:0];
        [_payBtn setBackgroundImage:[UIImage imageNamed:@"meat_cart_pay_right"] forState:UIControlStateNormal];
        _payBtn.titleEdgeInsets = UIEdgeInsetsMake(-TJ_SafeAreaBottomHeight/2.0, 0, 0, 0);
        _payBtn.isNeedAnimation = YES;
        
        //kWeakSelf(self);
        _payBtn.tapBlock = ^{
            
        };
    }
    return _payBtn;
}


- (UILabel *)payLab{
    
    if (!_payLab) {
        _payLab = [[UILabel alloc] init];
    }
    return _payLab;
}


- (UILabel *)cartTipNumberLab{
    
    if (!_cartTipNumberLab) {
        _cartTipNumberLab = [UILabel makeLabelWithText:@"1" textColor:KWhiteColor font:mMediumFont(11) textAliment:1];
        _cartTipNumberLab.backgroundColor = UIColorFromRGB(0xEE5B5B);
        [self addShadowAndCircleCorner:_cartTipNumberLab.layer corner:9];
        _cartTipNumberLab.layer.borderColor = KWhiteColor.CGColor;
        _cartTipNumberLab.layer.borderWidth = 1.0f;
        _cartTipNumberLab.hidden = YES;
        
    }
    return _cartTipNumberLab;
}


#pragma mark - other methods

- (NSMutableAttributedString *)getPayText:(NSString *)text{
    
    NSString *payTipS = @"合计金额：";
    NSString *moneySymbol = @"¥";
    NSString *totalS = [NSString stringWithFormat:@"%@%@%@", payTipS, moneySymbol, text];
    
    NSMutableAttributedString *attributedS = [[NSMutableAttributedString alloc] initWithString:totalS];
    
    [attributedS addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x333333) range:NSMakeRange(0, 5)];
    [attributedS addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xF25C62) range:NSMakeRange(5, totalS.length -5)];
    [attributedS addAttribute:NSFontAttributeName value:mFont(13) range:NSMakeRange(0, 5)];
    [attributedS addAttribute:NSFontAttributeName value:mFont(12) range:NSMakeRange(5, 1)];
    [attributedS addAttribute:NSFontAttributeName value:mMediumFont(17) range:NSMakeRange(6, totalS.length - 6)];

    return attributedS;
}


@end
